package com.indexingsystem.boss.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.indexingsystem.boss.entity.Pusers;

/**
 * MD5加密工具类.
 * 
 * @author Administrator
 *
 */
public class MD5Util {
	
	
	/**
	 * 加工密码，和登录一致。
	 * @param user
	 * @return
	 */
	public static Pusers md5Pswd(Pusers user){
		//密码为   email + '#' + pswd，然后MD5
		user.setPassword(md5Pswd(user.getUserName(),user.getPassword()));
		return user;
	}
	/**
	 * 字符串返回值
	 * @param email
	 * @param pswd
	 * @return
	 */
	public static String md5Pswd(String userName ,String pswd){
		pswd = String.format("%s#%s", userName,pswd);
		pswd = MathUtil.getMD5(pswd);
		return pswd;
	}

	/**
	 * 字符串加密生成MD5密文.
	 * 
	 * @param str
	 * @return
	 */
	public static String getMD5Str(String str) {
		MessageDigest messageDigest = null;
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(str.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			System.out.println("NoSuchAlgorithmException caught!");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		byte[] byteArray = messageDigest.digest();
		StringBuffer md5StrBuff = new StringBuffer();
		for (int i = 0; i < byteArray.length; i++) {
			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
				md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
			else
				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
		}
		return md5StrBuff.toString();
	}
	
	
	/**
	 * 解密.
	 * @param str
	 * @return
	 */
	public static String decode(String str) {
		MessageDigest messageDigest = null;
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(str.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			System.out.println("NoSuchAlgorithmException caught!");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		byte[] byteArray = messageDigest.digest();
		StringBuffer md5StrBuff = new StringBuffer();
		for (int i = 0; i < byteArray.length; i++) {
			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
				md5StrBuff.append("0").append(
						Integer.toHexString(0xFF & byteArray[i]));
			else
				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
		}
		return md5StrBuff.toString();
	}
	
	
	/**
	 * 加密
	 * @param str
	 * @return
	 */
	public static String encode(String str) {
		MessageDigest messageDigest = null;
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(str.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			System.out.println("NoSuchAlgorithmException caught!");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		byte[] byteArray = messageDigest.digest();
		StringBuffer md5StrBuff = new StringBuffer();
		for (int i = 0; i < byteArray.length; i++) {
			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
				md5StrBuff.append("0").append(
						Integer.toHexString(0xFF & byteArray[i]));
			else
				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
		}
		return md5StrBuff.toString();
	}

	
	/**
	 * 随机生成MD5加密.
	 * @return
	 */
	public static String generateSalt() {
		String chars = "0123456789abcdefghijklmnopqrstuvwxyz";
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < 6; i++) {
			int rand = (int) (Math.random() * chars.length());
			sb.append(chars.charAt(rand));
		}
		return sb.toString();
	}

	public static void main(String[] args) throws NoSuchAlgorithmException,
			UnsupportedEncodingException {
		String value = MD5Util.getMD5Str(MD5Util.getMD5Str("123456")+ "admin");
		System.out.println("加密用户名和密码3333="+value);
		
		String value444 = MD5Util.getMD5Str(MD5Util.getMD5Str("123456")+ "admin");
		System.out.println("加密用户名和密码4444="+value444);
		  
		  String str = "123456";
		  String md5=MD5Util.encode(str);
		  System.out.println("解密："+md5);
		  System.out.println("解密："+MD5Util.decode(md5));
		  
		  
		  
		  
		  String jjmvalue = MD5Util.getMD5Str("123456"+ "admin");
		  System.out.println("加密用户名和密码="+jjmvalue);
		  
		  String jmmValue = MD5Util.decode(jjmvalue);
		  System.out.println("解密用户名和密码1111="+jmmValue);
		  
		  String jmValue = MD5Util.decode(MD5Util.decode(value));
		  System.out.println("解密用户名和密码22222="+jmValue);


	}
}